/*
 * This is the MIT license, see also http://www.opensource.org/licenses/mit-license.html
 *
 * Copyright (c) 2001 Brian Pitcher
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

// $Header: /cvsroot/weblech/weblech/src/weblech/spider/URLObject.java,v 1.3 2002/06/02 08:00:48 weblech Exp $

package wendyeq.iweb.blog.util.spider;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.security.MessageDigest;
import java.util.Date;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import wendyeq.iweb.blog.pojo.Article;

public class URLObject{
	private static final Logger _logClass = Logger.getLogger(URLObject.class);
	private final URL sourceURL;
	private final String contentType;
	private final byte[] content;


	public URLObject(URL sourceURL, String contentType, byte[] content, SpiderConfig config)
	{
		this.sourceURL = sourceURL;
		this.contentType = contentType;
		this.content = content;
	}

	public URLObject(URL sourceURL, SpiderConfig config){
		this.sourceURL = sourceURL;
		String s = sourceURL.toExternalForm().toLowerCase();
		if(s.indexOf(".jpg") != -1){
			contentType = "image/jpeg";
		}else if(s.indexOf(".gif") != -1){
			contentType = "image/gif";
		}else{
			contentType = "text/html";
		}
		content = new byte[0];
		//        if(existsOnDisk()){
		//            File f = new File(convertToFileName());
		//            if(f.isDirectory()){
		//                f = new File(f, "index.html");
		//            }
		//            content = new byte[(int) f.length()];
		//            try{
		//                FileInputStream in = new FileInputStream(f);
		//                in.read(content);
		//                in.close();
		//            }catch(IOException ioe){
		//                _logClass.warn("IO Exception reading disk version of URL " + sourceURL, ioe);
		//            }
		//        }else{
		//            content = new byte[0];
		//        }
	}
	/****
	 * 将Page中的信息进行过滤
	 * */
	public String parseArticleInDocument( String textContent){
		if(sourceURL.getPath().contains("htm_data")){
			Document doc = Jsoup.parse(textContent); 
			Element div = doc.getElementById("subject_tpc");
			StringBuilder sb=new StringBuilder();
			sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\"> <head><meta http-equiv=Content-Type content=\"text/html;charset=utf-8\"> </head><title>");
			sb.append(div.text());
			sb.append("</title> <body>");
			sb.append(doc.getElementsByClass("tpc_content").first().html());
			sb.append("</body> </html>");
			return sb.toString();
		}
		return textContent;
	}
	public String md5(String source) {
		StringBuffer sb = new StringBuffer(32);
		try {
			MessageDigest md 	= MessageDigest.getInstance("MD5");
			byte[] array 		= md.digest(source.getBytes("utf-8"));
			for (int i = 0; i < array.length; i++) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3));
			}
		} catch (Exception e) {
			_logClass.error("Can not encode the string '" + source + "' to MD5!", e);
			return null;
		}
		return sb.toString();
	}

	/****
	 * 将Page中的信息进行过滤
	 * */
	public Article parse2Article(){
		Article a=new Article();
		_logClass.info("url==>"+sourceURL.toString());
		if(sourceURL.getPath().contains("htm_data")&&!sourceURL.toString().endsWith("/")){
			Document doc;
			try {
				doc = Jsoup.parse(new String(content,"gbk"));
				Element div = doc.getElementById("subject_tpc");
				a.setTitle(div.text());
				a.setAlias(md5(sourceURL.toString()));
				a.setContent(doc.getElementsByClass("tpc_content").first().html());
				a.setCategory("AsianNo");
				a.setIsComment(false);
				a.setCreatedTime(new Date());
				return a;
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		return null;
	}
	public String getContentType()
	{
		return contentType;
	}

	public boolean isHTML()
	{
		return contentType.toLowerCase().startsWith("text/html");
	}

	public boolean isXML()
	{
		return contentType.toLowerCase().startsWith("text/xml");
	}

	public boolean isImage()
	{
		return contentType.startsWith("image/");
	}

	public String getStringContent()
	{
		return new String(content);
	}

	//    private String convertToFileName()
	//    {
	//        String url = sourceURL.toExternalForm();
	//        int httpIdx = url.indexOf("http://");
	//        if(httpIdx == 0)
	//        {
	//            url = url.substring(7);
	//        }
	//        // Check for at least one slash -- otherwise host name (e.g. sourceforge.net)
	//        if(url.indexOf("/") < 0)
	//        {
	//            url = url + "/";
	//        }
	//        // If trailing slash, add index.html as default
	//        if(url.endsWith("/"))
	//        {
	//            url = url + "index.html";
	//        }
	//        if(!url.endsWith(".html")){
	//        	url+=".html";
	//        }
	//        
	//        url = textReplace("?", URLEncoder.encode("?"), url);
	//        url = textReplace("&", URLEncoder.encode("&"), url);
	//        return config.getSaveRootDirectory().getPath() + "/" + url;
	//    }

	//    public boolean existsOnDisk() {
	//        File f = new File(convertToFileName());
	//        return (f.exists() && !f.isDirectory());
	//    }

	//    public void writeToDB()
	//    {
	//    	writeToDB(convertToFileName());
	//    }
	//
	//    public void writeToDB(String fileName)
	//    {
	//        _logClass.debug("writeToFile(" + fileName + ")");
	//        try
	//        {
	//            File f = new File(fileName);
	//            f.getParentFile().mkdirs();
	//            FileOutputStream out = new FileOutputStream(fileName);
	//            out.write(content);
	//            out.flush();
	//            out.close();
	//        }
	//        catch(IOException ioe)
	//        {
	//            _logClass.warn("IO Exception writing to " + fileName, ioe);
	//        }
	//    }
	//    public void writeToFile(String fileName)
	//	{
	//		_logClass.info("===============================");
	//		_logClass.info("writeToFile(" + fileName + ")");
	//
	//		try
	//		{
	//			File f = new File(fileName);
	//			f.getParentFile().mkdirs();
	//			FileOutputStream out = new FileOutputStream(fileName);
	//			out.write(parseArticleInDocument(new String(content,"gbk")).getBytes());
	//			out.flush();
	//			out.close();
	//		}
	//		catch(IOException ioe)
	//		{
	//			_logClass.warn("IO Exception writing to " + fileName, ioe);
	//		}
	//	}
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("URLObject: ");
		sb.append(contentType);
		sb.append("\n");
		sb.append(getStringContent());
		return sb.toString();
	}

}
